#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#include <Windows.h>
#include <Commctrl.h>
#pragma comment (lib, "Comctl32.lib")

#include <string>
#include <functional>
#include <memory>

#include "detours.h"
#pragma comment (lib, "detours.lib")

#include "nrapi"
#pragma comment (lib, "nrcore.lib")

#include "..\spimpl\resource.h"

using namespace std;


int WINAPI WinMain(
	__in HINSTANCE hInstance,
	__in_opt HINSTANCE hPrevInstance,
	__in LPSTR lpCmdLine,
	__in int nShowCmd)
{
	InitCommonControls();

	HMODULE hImpl = LoadLibraryExW(L"spimpl", NULL, LOAD_LIBRARY_AS_DATAFILE);
	if(!hImpl)
	{
		MessageBox::Show(L"Cannot load essential module 'spimpl.dll'", L"Fatal error", MB_OK | MB_ICONERROR);
		return -1;
	}

	wstring s = _wgetenv(L"windir");
	s += L"\\notepad.exe";

	PROCESS_INFORMATION pi = {0};
	STARTUPINFO si = {0};
	si.cb = sizeof(si);

	BOOL ret = 
	DetourCreateProcessWithDllW(
		s.c_str(),
		NULL,
		NULL,
		NULL,
		TRUE,
		CREATE_DEFAULT_ERROR_MODE,
		NULL,
		NULL,
		&si,
		&pi,
		NULL,
		"spimpl",
		NULL);

	if(!ret)
	{
		MessageBox::Show(IDS_ERR_LAUNCH, hImpl, IDS_APPLICATION, hImpl, MB_OK | MB_ICONERROR);
	}

	return 0;
}